﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace RuoTeng.Windows.Forms
{
    public partial class Notice : RuoTengControl
    {

     /// <summary>
     /// 获取授权信息函数句柄
     /// </summary>
        public delegate void ReponseServerHandle();
     /// <summary>
     /// 订阅获取远程授权信息函数
     /// </summary>
        public event ReponseServerHandle  ReponseServerFunc = null; 

        public Notice()
        {
            this.CreatePictureBox(RuoTeng.Windows.Forms.Resources.Announce, "pictureBox_Notice");
            InitializeComponent();
        }

    /// <summary>
    ///重写授权处理函数 公告模块授权信息处理函数
    /// </summary>
    /// <param name="sort"></param>
        public override void Authorization(SortedList<string, object> sort)
        {
            string notes = GetValue("notes","如果公告没有显示，请点击重新获取信息",sort);
            this.richTextBox_update_log.Text = notes;
            base.Authorization(sort);
        }

     /// <summary>
     /// 单击 重新获取信息 Button事件
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.ReponseServerFunc != null)
            {
                this.button1.Text = "数据获取中...";
        //由于考虑到性能和连续请求影响,当请求过后,禁用按钮,直至上一次请求完成。再开启按钮。
                this.button1.Enabled = false;
                System.Threading.Thread thread = new System.Threading.Thread(Request);
                try
                {             
                    thread.Start();
                }
                catch { }
            }
        }

       /// <summary>
       /// 检测是否能够开启 重新获取公告信息 按钮
       /// </summary>
       /// <param name="isEnabled"></param>
        private void EnabledGetServer(bool isEnabled)
        {
            if (isEnabled)
            {
                this.button1.Text = "重新获取信息";
                this.button1.Enabled = true;
            }
        }

        /// <summary>
        /// 请求获取服务器授权信息,具体实现代码由订阅该事件的函数来实现
        /// </summary>
        private void Request()
        {
            if (this.ReponseServerFunc != null)
            {
                this.ReponseServerFunc();
                this.EnabledGetServer(true);
            }
        }

        /// <summary>
        /// 实现richtext中超链接点击能够打开跳转到相应的网址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void richTextBox_update_log_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            try
            {
                if (e.LinkText != "")
                {  
                //检测是否http https协议开头的网址
                    System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(e.LinkText, @"(?:http|https)://[\S]+", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    if (match.Value != null && match.Value != "")
                    {
                        RuoTeng.Plugin.Web.ShellExecute(
                            IntPtr.Zero,
                            "open",
                            match.Value, 
                            null, 
                            null,
                            RuoTeng.Plugin.Web.ShowCommands.SW_SHOWNOACTIVATE);
                    }
                }
            }
            catch{}
        }       
    }
}
